package com.example.a513;

import android.Manifest;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;
import java.io.IOException;

public class PlayAudioTest extends AppCompatActivity implements View.OnClickListener {
    private Button btn_play;
    private Button btn_stop;
    private Button btn_pause;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_play_audio_test);

        btn_pause = findViewById(R.id.pause);
        btn_play =  findViewById(R.id.play);
        btn_stop = findViewById(R.id.stop);

        btn_play.setOnClickListener(this);
        btn_pause.setOnClickListener(this);
        btn_stop.setOnClickListener(this);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
        {
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
        else {
            intiPlayer();
        }
    }

    private void intiPlayer() {
        try {
            File file = new File(Environment.getExternalStorageDirectory(),"mm.mp3");
            Log.e("TAG", "intiPlayer: "+file );
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.prepare();
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 1:
                if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    intiPlayer();
                }else{
                    //
                }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.play){
            mediaPlayer.start();
        }else if (v.getId() == R.id.pause){
            if (mediaPlayer.isPlaying()){
                mediaPlayer.pause();
            }
        }else if (v.getId() == R.id.stop){
            if (mediaPlayer.isPlaying()){
                mediaPlayer.stop();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}